#include "CCanvas.h"

#include <iostream>
#include <fstream>
using namespace std;

#include "glext.h"

#include "block.h"
#include "blockmatrix.h"

//-----------------------------------------------------------------------------

void CCanvas::initializeGL()
{
    glClearColor(1.0f, 1.0f, 1.0f, 0.5f);			          // White Background
    glClearDepth(1.0f);									                // Depth Buffer Setup
    glEnable(GL_DEPTH_TEST);							              // Enables Depth Testing
    glDepthFunc(GL_LEQUAL);								              // The Type Of Depth Testing To Do
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations
};

//-----------------------------------------------------------------------------

void CCanvas::resizeGL(int width, int height)
{
    // set up the window-to-viewport transformation
    glViewport( 0,0, width,height );

    // vertical camera opening angle
    double beta = 90.0;

    // aspect ratio
    double gamma;
    if (height > 0)
        gamma = width/(double)height;
    else
        gamma = width;

    // front and back clipping plane at
    double n = -1.0;
    double f = -20.0;

    // set projection matrix
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective( beta, gamma, -n, -f );

};

void CCanvas::paintGL()
{
    // clear screen and depth buffer
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // set model-view matrix
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt( 0,0,0,  0,0,-1,  0,1,0 );     // camera position , "look at" point , view-up vector

    // set up a light source
    GLfloat ambient [] = { 0.6, 0.6, 0.6, 1.0 };
    GLfloat diffuse [] = { 1.0, 1.0, 1.0, 1.0 };
    GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 };
    GLfloat position[] = { -10.0, 5.0, 25.0, 10.0 };

    glLightfv( GL_LIGHT0, GL_AMBIENT,  ambient  );
    glLightfv( GL_LIGHT0, GL_DIFFUSE,  diffuse  );
    glLightfv( GL_LIGHT0, GL_SPECULAR, specular );
    glLightfv( GL_LIGHT0, GL_POSITION, position );

    // turn on light
    glEnable( GL_NORMALIZE );
    glEnable( GL_LIGHTING );
    glEnable( GL_LIGHT0 );

    // choose between flat and Gouraud shading
//    glShadeModel( GL_FLAT );
    glShadeModel( GL_SMOOTH );

    // set up a sphere
    GLUquadricObj *sphere;
    sphere = gluNewQuadric();
    gluQuadricDrawStyle( sphere, GLU_FILL );

    // set material coefficients
    GLfloat mat_emission[] = { 0.0, 0.0, 0.0, 0.0 };
    GLfloat mat_ambient [] = { 0.2, 0.2, 0.2, 1.0 };
    GLfloat mat_diffuse [] = { 0.8, 0.2, 0.2, 1.0 };
    GLfloat mat_specular[] = { 0.5, 0.5, 0.5, 1.0 };

    glMaterialfv( GL_FRONT, GL_EMISSION, mat_emission );
    glMaterialfv( GL_FRONT, GL_AMBIENT,  mat_ambient  );
    glMaterialfv( GL_FRONT, GL_DIFFUSE,  mat_diffuse  );
    glMaterialfv( GL_FRONT, GL_SPECULAR, mat_specular );
    glMaterialf ( GL_FRONT, GL_SHININESS, 40.0 );

    GLUquadric* qobj = gluNewQuadric();
    gluQuadricNormals(qobj, GLU_FILL);

    glPushMatrix() ;
    glTranslated(0,3.0,-7.0);
    glScaled (2.0,2.0,2.0);
    glRotated(90, 1,0,0);
    // cima
    gluCylinder(qobj, 1.5, 1.5, 0.15, 100, 10);
    // cilindrone
    gluCylinder(qobj, 1.0, 1.0, 3.5, 100, 10);
    gluDisk(qobj, 0,1.5,100,10);
    glPopMatrix();
    glPushMatrix();
    glTranslated(0,2.75,-7.0);
    glScaled (2.0,2.0,2.0);
    glRotated(90, 1,0,0);
    gluDisk(qobj, 0,1.5,100,10);

    glPopMatrix();

    //Block b = Block(1.0, 1.5, 20, 100, Point3d(10,10,10));
    //b.drawBlock();

    BlockMatrix bmatrix = BlockMatrix(3.5, 15, 0.5);
    bmatrix.insert(0, 0);

    bmatrix.insert(2, 4);
    bmatrix.insert(2, 22);
    bmatrix.insert(2, 0);
    bmatrix.insert(3, 7);
    bmatrix.drawMatrix();

    tau += 0.5;
}
